package org.dromara.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 首页
 *
 * @author Lion Li
 */
@SaIgnore
@RequiredArgsConstructor
@RestController
public class IndexController {

    @Autowired
    private TestMapper testMapper;

    /**
     * 访问首页，提示语
     */
    @GetMapping("/")
    public String index() {
        return StringUtils.format("欢迎使用{}后台管理框架，请通过前端地址访问。", SpringUtils.getApplicationName());
    }

    @GetMapping("/t")
    public String test1() {
        String dateStr = DateTime.now().toString(DatePattern.PURE_DATE_PATTERN);
        testMapper.updateSQL("select * from monitor_point_data_hour limit 1");
        return "OK";
    }

    @GetMapping("/test")
    public String test() {
        String dateStr = DateTime.now().toString(DatePattern.PURE_DATE_PATTERN);
        Date now = new Date();
        Date targetDate = DateUtil.truncate(DateUtil.offsetDay(now, 3), DateField.DAY_OF_WEEK);
        String start = DateUtil.offsetDay(targetDate, -5).toString(DatePattern.NORM_DATETIME_PATTERN);
        String end = DateUtil.offsetDay(targetDate, -4).toString(DatePattern.NORM_DATETIME_PATTERN);
        String detailSQL = """
            SUBMIT /*+set_var(query_timeout=100000)*/ TASK monitor_point_data{date}_async
            AS INSERT OVERWRITE qz_live_data_gen_every_day.monitor_point_data
            SELECT * FROM qz_live_data.monitor_point_data where happen_time >= '{start}' and happen_time < '{end}'
            """;

        String minuteSQL = """
            SUBMIT TASK monitor_point_data_minute{date}_async
            AS INSERT OVERWRITE qz_live_data_gen_every_day.monitor_point_data_minute
            SELECT * FROM qz_live_data.monitor_point_data_minute where happen_time >= '{start}' and happen_time < '{end}'
            """;

        String hourSQL = """
            SUBMIT TASK monitor_point_data_hour{date}_async
            AS INSERT OVERWRITE qz_live_data_gen_every_day.monitor_point_data_hour
            SELECT * FROM qz_live_data.monitor_point_data_hour where where happen_time >= '{start}' and happen_time < '{end}'
            """;

        detailSQL = detailSQL.replace("{date}", dateStr).replace("{start}", start).replace("{end}", end);
        minuteSQL = minuteSQL.replace("{date}", dateStr).replace("{start}", start).replace("{end}", end);
        hourSQL = hourSQL.replace("{date}", dateStr).replace("{start}", start).replace("{end}", end);

        testMapper.updateSQL(detailSQL);
        testMapper.updateSQL(minuteSQL);
        testMapper.updateSQL(hourSQL);
        return "OK";
    }

}
